//
//  BCLaunchAdsController.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/26.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCLaunchAdsController.h"

@interface BCLaunchAdsController ()

@property (nonatomic, strong) UIImageView *adsIV;


/**
 *iconBackgroundView
 */
@property (nonatomic, strong) UIView *iconBackgroundView;

/**
 *iconIV
 */
@property (nonatomic, strong) UIImageView *iconIV;


/**
 *skipButton
 */
@property (nonatomic, strong) QMUIButton *skipButton;

/**
 *BCLaunchAdsControllerCompleteBlock
 */
@property (nonatomic, copy) BCLaunchAdsControllerCompleteBlock completeBlock;

/**
 *BCAdsDataModel
 */
@property (nonatomic, strong) BCAdsDataModel *adModel;


/**
 *countdownTime
 */
@property (nonatomic, assign) NSTimeInterval countdownTime;

@end

@implementation BCLaunchAdsController

+ (void)showFromController:(UIViewController *)fromController adModel:(BCAdsDataModel *)adModel complete:(BCLaunchAdsControllerCompleteBlock)complete {
    BControllerWindow *window = [[BControllerWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    BCLaunchAdsController *vc = [BCLaunchAdsController new];
    vc.showWindow = window;
    vc.fromController = fromController;
    vc.completeBlock = complete;
    vc.adModel = adModel;
    window.rootViewController = vc;
    [window makeKeyAndVisible];
}


- (QMUIButton *)skipButton {
    if (!_skipButton) {
        _skipButton = [QMUIButton new];
//        [_skipButton setTitle:BCLocalizedKey(@"") forState:<#(UIControlState)#>]
        [_skipButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _skipButton.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
        _skipButton.contentEdgeInsets = UIEdgeInsetsMake(5, 10, 5, 10);
        _skipButton.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        [self.view addSubview:_skipButton];
        //
        [_skipButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.view).offset(-15.0);
            make.centerY.mas_equalTo(self.view.mas_top).offset(Height_StatusBar+44.0/2);
        }];
        [self.view layoutIfNeeded];
        //
        [_skipButton addTarget:self action:@selector(skipButtonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _skipButton;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    //
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupSubviews];
}

- (void)setupSubviews {
    self.iconBackgroundView = [UIView new];
    self.iconBackgroundView.clipsToBounds = true;
    [self.view addSubview:self.iconBackgroundView];
    [self.iconBackgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view);
        make.width.mas_equalTo(self.view);
        make.height.mas_equalTo(100);
        if (@available(iOS 11.0,*)) {
            make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        }else {
            make.bottom.mas_equalTo(self.view);
        }
    }];
    //
    self.iconIV = [UIImageView new];
    [self.iconBackgroundView addSubview:self.iconIV];
    [self.iconIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.mas_equalTo(self.iconBackgroundView);
    }];
    self.iconIV.image = LOAD_IMAGE(@"ad_launch_logo");
    
    //
    self.adsIV = [UIImageView new];
    self.adsIV.clipsToBounds = true;
    [self.view addSubview:self.adsIV];
    [self.adsIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.iconBackgroundView.mas_top);
    }];
//    self.adsIV.backgroundColor = [UIColor blackColor];
//    self.adsIV.contentMode = UIViewContentModeScaleAspectFill;
    
    //加载广告图片
    [self loadAdIVImageAction];
}
- (void)loadAdIVImageAction {
    BCAdsDataModel *adModel = self.adModel;
    if (adModel) {
        [self loadAdIVImageActionFromAdModel:adModel];
    }else {
        [self skipButtonDidClickedAction];
    }
}
- (void)loadAdIVImageActionFromAdModel:(BCAdsDataModel *)adModel {
    //判断是否image存在
    UIImage *adImage = [[SDImageCache sharedImageCache] imageFromCacheForKey:adModel.image_url];
    if (adImage) {
        self.adsIV.image = adImage;
        [self startSkipCountdownAction];
    }else {//重新加载
        WEAK_SELF
        [self.adsIV sd_setImageWithURL:[NSURL URLWithString:adModel.image_url] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            if (error) {
                [wself skipButtonDidClickedAction];
            }
            if (image) {
                [wself startSkipCountdownAction];
            }
        }];
    }
}

#pragma mark - 跳过倒计时
- (void)startSkipCountdownAction {
    self.countdownTime = 5;
    [self repeatCountdownAction];
}
- (void)repeatCountdownAction {
    if (self) {
        NSString *title = BCLocalizedFormatString(@"Skip %@",@(self.countdownTime));
        [self.skipButton setTitle:title forState:UIControlStateNormal];
        [self.skipButton sizeToFit];
        self.skipButton.layer.cornerRadius = CGRectGetHeight(self.skipButton.frame)/2;
        if (self.countdownTime==0) {
            [self skipButtonDidClickedAction];
        }else {
            self.countdownTime -= 1;
            [self performSelector:@selector(repeatCountdownAction) withObject:nil afterDelay:1.0];
        }
    }
}

//
#pragma mark - skipButtonDidClickedAction
- (void)skipButtonDidClickedAction {
    [_skipButton setAlpha:0];
    [self.view setUserInteractionEnabled:false];
    self.view.backgroundColor = [UIColor clearColor];
    if (self.completeBlock) {
        self.completeBlock();
    }
    [UIView animateWithDuration:0.6 animations:^{
        self.view.alpha = 0;
    } completion:^(BOOL finished) {
        [self dismissWindow];
    }];
}

@end
